package com.huitao.jpegdemo

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.graphics.RectF
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.huitao.jpegdemo.databinding.ActivityMainBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.logging.Logger

private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var surfaceHolder: SurfaceHolder
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        surfaceHolder = binding.surfaceView.holder
        surfaceHolder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                startPreview()
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {

            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {

            }

        })

        binding.btStart.setOnClickListener{
            startPreview()
        }

    }

    private fun startPreview() {
        val file = File(cacheDir, "aaa.mjpeg")
        if (file.exists() && file.isFile) {
            Log.d(TAG, "文件路劲   onCreate: ${file.path}")
        }
        lifecycleScope.launch {
           // nativeDecodeMJPEG(file.path, surfaceHolder.surface)
            readJpeg(file) { byte ->
                val canvas = surfaceHolder.lockCanvas()
                val bitmap = BitmapFactory.decodeByteArray(byte, 0, byte.size)
                canvas.apply {
                    drawBitmap(bitmap, Matrix().apply {
                        setRectToRect(
                            RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()),
                            RectF(0f, 0f, width.toFloat(), height.toFloat()),
                            Matrix.ScaleToFit.CENTER
                        )
                    }, null)
                }
                surfaceHolder.unlockCanvasAndPost(canvas)
                bitmap.recycle()
            }

        }

    }

    override fun onDestroy() {
        super.onDestroy()
       binding.surfaceView.holder.surface.release()
    }

    /**
     * A native method that is implemented by the 'jpegdemo' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    external fun nativeCompressJPEG(bitmap: Bitmap, quality: Int, outPath: String): Int

    external fun nativeDecodeMJPEG(path: String, surface: Surface): Bitmap

    companion object {
        // Used to load the 'jpegdemo' library on application startup.
        init {
            System.loadLibrary("jpegdemo")
        }
    }
}